package com.awz.manage.aop.annotation;
/*
 * @author  LiangYu
 * @date  2022/1/7 0:40
 * @Email:salong0503@aliyun.com
 */


import org.springframework.aop.support.AopUtils;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class ExpressionEvaluator<T> extends CachedExpressionEvaluator {
	private final ParameterNameDiscoverer paramNameDiscoverer = new DefaultParameterNameDiscoverer();
	private final Map<ExpressionKey, Expression> conditionCache = new ConcurrentHashMap<>(64);
	private final Map<AnnotatedElementKey, Method> targetMethodCache = new ConcurrentHashMap<>(64);
	
	
	public EvaluationContext createEvaluationContext(Object object, Class<?> targetClass, Method method, Object[] args) {
		Method targetMethod = getTargetMethod(targetClass, method);
		ExpressionRootObject root = new ExpressionRootObject(object, args);
		return new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
	}
	
	
	public T condition(String conditionExpression, AnnotatedElementKey elementKey, EvaluationContext evalContext, Class<T> clazz) {
		return getExpression(this.conditionCache, elementKey, conditionExpression).getValue(evalContext, clazz);
	}
	
	private Method getTargetMethod(Class<?> targetClass, Method method) {
		AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
		Method targetMethod = this.targetMethodCache.get(methodKey);
		if (targetMethod == null) {
			targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
			if (targetMethod == null) {
				targetMethod = method;
			}
			this.targetMethodCache.put(methodKey, targetMethod);
		}
		return targetMethod;
	}
}
